package com.zmm.myyimixian.ui.activity.address;

import android.content.Intent;
import android.view.View;

import com.zmm.myyimixian.R;
import com.zmm.myyimixian.base.BaseActivity;
import com.zmm.myyimixian.widget.wheel.OnWheelChangedListener;
import com.zmm.myyimixian.widget.wheel.WheelView;
import com.zmm.myyimixian.widget.wheel.adapters.ArrayWheelAdapter;

import butterknife.BindView;
import butterknife.OnClick;

public class WheelActivity extends BaseActivity implements View.OnClickListener, OnWheelChangedListener {
    @BindView(R.id.id_province)
    WheelView mViewProvince;
    @BindView(R.id.id_city)
    WheelView mViewCity;
    @BindView(R.id.id_district)
    WheelView mViewDistrict;

    @Override
    protected void initView() {
        setUpListener();
        setUpData();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_wheel;
    }

    private void setUpListener() {
        // 省份监听
        mViewProvince.addChangingListener(this);
        // 城市监听
        mViewCity.addChangingListener(this);
        // 区县监听
        mViewDistrict.addChangingListener(this);
    }

    private void setUpData() {
        initProvinceDatas();
        mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(WheelActivity.this, mProvinceDatas));
        // 设置显示的列数
        mViewProvince.setVisibleItems(5);
        mViewCity.setVisibleItems(5);
        mViewDistrict.setVisibleItems(5);
        updateCities();
        updateAreas();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewProvince) {
            updateCities();
        } else if (wheel == mViewCity) {
            updateAreas();
        } else if (wheel == mViewDistrict) {
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
            mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
        }
    }

    /**
     *更新区县数据
     */
    private void updateAreas() {
        int pCurrent = mViewCity.getCurrentItem();
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
        String[] areas = mDistrictDatasMap.get(mCurrentCityName);

        if (areas == null) {
            areas = new String[]{""};
        }
        mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));
        mViewDistrict.setCurrentItem(0);
    }

    /**
     * 更新城市
     */
    private void updateCities() {
        int pCurrent = mViewProvince.getCurrentItem();
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[]{""};
        }
        mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
        mViewCity.setCurrentItem(0);
        updateAreas();
    }



    @OnClick({R.id.wheel_cancle, R.id.wheel_confirm,R.id.wheel_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wheel_cancle:
                finish();
                break;
            case R.id.wheel_confirm:
                //回传地址
                Intent intent=new Intent();
                intent.putExtra("province",mCurrentProviceName);
                intent.putExtra("city",mCurrentCityName);
                intent.putExtra("district",mCurrentDistrictName);
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.wheel_layout:
               finish();
                break;
        }
    }
}
